<template>
  <div>
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
>
<comment-item  v-for="(item,index) in list" :key="index"  :comment="item">
</comment-item>
  <!-- <van-cell v-for="(item,index) in list" :key="index" :title="item.content" /> -->
</van-list>
  </div>
</template>

<script>
import commentItem from './comment-iten.vue'
import { getCommentsAPI } from '../../../api/index.js'
export default {
  name: 'commentList',
  components: {
    commentItem
  },
  props: {
    sourceId: {
      type: String, // 源id，文章id或评论id
      default: ''
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,

      offset: null// 翻页偏移量，如果有id,根据id找后续的列表数据
    }
  },
  methods: {
    async onLoad () {
      try {
        const res = await getCommentsAPI({
          type: 'a',
          source: this.sourceId,
          offset: this.offset,
          limit: 3
        })
        console.log(res)
        this.list.push(...res.data.data.results)
        this.$emit('onLoad-success', res.data.data.total_count)
        this.loading = false
        if (res.data.data.results.length === 0) {
          this.finished = true
        } else {
          this.offset = res.data.data.last_id
        }
      } catch (error) {
        this.loading = false
        console.log(error)
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
